/*
 *  message_receiver.cpp
 */

#include <string>
#include <vector>
#include "debug.h"
#include "lock.h"
#include "message.h"
#include "message_queue.h"
#include "message_receiver.h"
#include "protocol.h"
#include "session.h"
#include "socket.h"
#include "socket_exception.h"

MessageReceiver::MessageReceiver(Session* session, MessageQueue& q) : q(q) {
    this->session = session;
}

MessageReceiver::~MessageReceiver() {}

void MessageReceiver::run() {
    Message* msg;
    while (session->isAlive()) {
        msg = session->getProtocol().recvMessage(*(session->getSocket()));
        if (msg != NULL) {
            PRINT(msg->toString());
            q.push(msg);
        } else {
            session->kill();
            q.push(new Message(session->getID(), Protocol::MSG_DISCONNECTED));
        }
    }
}

//{+}

